package pl.pelcra.cesar.spider.data;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

import org.apache.log4j.Logger;

import pl.pelcra.cesar.data.interfaces.ICommonData;
import pl.pelcra.cesar.webrecorder.provision.WebRecordThreadData;

/**
 * Class stores list of class MapLangUrl
 * 
 * @author margielewski
 * 
 */
public class SpiderThreadDataNew implements ICommonData<MapLangUrl> {

	private Boolean isDataPutting = true;
	private final BlockingQueue<MapLangUrl> listValues = new ArrayBlockingQueue<MapLangUrl>(
			2048, true);

	private final Logger logger = Logger.getLogger(WebRecordThreadData.class);

	/**
	 * set variable isDataPutting on false when isDataPutting is false it means
	 * that thread ends putting data to this class
	 */
	@Override
	public void endPutting() {
		this.isDataPutting = false;
	}

	/**
	 * return first record from list if not empty or return null object
	 * 
	 * @return return object MapLangUrl form list
	 */
	@Override
	public MapLangUrl get() {
		try {
			return this.listValues.take();
		} catch (InterruptedException e) {
			this.logger.info(e.getMessage());
			return null;
		}
	}

	/**
	 * @return return true - map is empty and thread end putting data on map,
	 *         return false - map is not empty or thread doesn't end putting
	 *         data to map
	 */
	@Override
	public boolean isEndPutting() {
		return this.listValues.isEmpty() && !this.isDataPutting;
	}

	/**
	 * put data to map
	 * 
	 * @param mapLangUrl
	 */
	@Override
	public void put(MapLangUrl value) {
		try {
			this.listValues.put(value);
			this.logger.info(String.format("SpiderThreadData %s URLs = %s",
					value.getId(), value.size()));
		} catch (InterruptedException e) {
			this.logger.info(e.getMessage());
		}
	}
}
